package com.park.web.controller;

import com.park.web.base.baseOperation.BaseController;
import com.park.web.core.wrapper.WrapMapper;
import com.park.web.core.wrapper.Wrapper;
import com.park.web.model.domain.pbaSystemConfig;
import com.park.web.service.pbaSystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(value = "WEB - PbaSystemConfigController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "PbaSystemConfig", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class PbaSystemConfigController extends BaseController {

    @Autowired
    pbaSystemConfigService ipbaSystemConfigService;

    @PostMapping("configAll")
    @ApiOperation(value = "获取所有配置", notes = "获取所有配置")
    public Wrapper getAll() {
        var list = ipbaSystemConfigService.selectAll();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        var listOrder = list.stream().collect(Collectors.groupingBy(pbaSystemConfig::getServiceCode, Collectors.partitioningBy(x -> x.getParamSettingsGroup() != null)));
        for (var temp : listOrder.entrySet()) {
            var groupOld = temp.getValue().get(true).stream().collect(Collectors.groupingBy(pbaSystemConfig::getParamSettingsGroup, Collectors.toMap(pbaSystemConfig::getDisplayKey, pbaSystemConfig::getDisplayValue)));
            var groupNew = temp.getValue().get(false).stream().collect(Collectors.toMap(pbaSystemConfig::getDisplayKey, pbaSystemConfig::getDisplayValue));
            Map<String, Object> map = new HashMap<String, Object>();
            map.putAll(groupOld);
            map.putAll(groupNew);
            resultMap.put(temp.getKey(), map);
        }
        return WrapMapper.okWithoutMessage(resultMap);
    }
}